package com.erge.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.erge.student.domain.bo.TreeNode;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.erge.common.annotation.Log;
import com.erge.common.core.controller.BaseController;
import com.erge.common.core.domain.AjaxResult;
import com.erge.common.enums.BusinessType;
import com.erge.student.domain.ClassDO;
import com.erge.student.service.IClassService;
import com.erge.common.utils.poi.ExcelUtil;

import lombok.RequiredArgsConstructor;

/**
 * 班级Controller
 *
 * @author roydon
 * @date 2025-05-22
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/student/class")
public class ClassController extends BaseController {
    private final IClassService classService;

    /**
     * 查询班级列表
     */
    @PreAuthorize("@ss.hasPermi('student:class:list')")
    @GetMapping("/list")
    public AjaxResult list(ClassDO classDO) {
        List<ClassDO> list = classService.selectClassList(classDO);
        return success(list);
    }

    /**
     * 查询班级列表
     */
    @GetMapping("/classTree")
    public AjaxResult classTree(ClassDO classDO) {
        List<TreeNode> list = classService.classTree(classDO);
        return success(list);
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('student:class:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClassDO classDO){
        List<ClassDO> list = classService.selectClassList(classDO);
        ExcelUtil<ClassDO> util = new ExcelUtil<ClassDO>(ClassDO.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(classService.selectClassById(id));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClassDO classDO) {
        return toAjax(classService.insertClass(classDO));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClassDO classDO) {
        return toAjax(classService.updateClass(classDO));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('student:class:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(classService.deleteClassByIds(ids));
    }
}
